package com.example.marketing.action;

import com.example.marketing.business.Coupon;
import com.example.marketing.business.Order;
import com.example.marketing.business.UserLevel;
import com.example.marketing.market.MarketingAction;
import com.example.marketing.market.MarketingResult;


import java.math.BigDecimal;
import java.util.List;
import java.util.function.Predicate;

/**
 * 复合条件和动作的实现示例
 */
public class CompositeConditionsAndActions {
    
    /**
     * 创建订单金额条件
     * @param threshold 金额门槛
     * @return 条件谓词
     */
    public static Predicate<Order> amountGreaterThan(BigDecimal threshold) {
        return order -> order.getTotalAmount().compareTo(threshold) >= 0;
    }
    
    /**
     * 创建用户等级条件
     * @param requiredLevel 要求的用户等级
     * @return 条件谓词
     */
    public static Predicate<Order> userLevelAtLeast(UserLevel requiredLevel) {
        return order -> order.getUser().getUserLevel().ordinal() >= requiredLevel.ordinal();
    }
    
    /**
     * 创建用户参与过抽奖的条件
     * @return 条件谓词
     */
    public static Predicate<Order> hasParticipatedLottery() {
        return Order::hasParticipatedLottery;
    }
    
    /**
     * 创建赠送优惠券的动作
     * @param coupon 优惠券
     * @return 营销动作
     */
    public static MarketingAction grantCouponAction(Coupon coupon) {
        return order -> {
            order.getUser().grantCoupon(coupon);
            return new MarketingResult(
                "COUPON_GRANT", 
                "赠送优惠券", 
                true, 
                "成功赠送优惠券: " + coupon.getCouponName()
            );
        };
    }
    
    /**
     * 创建增加积分的动作
     * @param points 积分数量
     * @return 营销动作
     */
    public static MarketingAction addPointsAction(int points) {
        return order -> {
            order.getUser().addPoints(points);
            return new MarketingResult(
                "POINTS_ADD", 
                "增加积分", 
                true, 
                "成功增加积分: " + points
            );
        };
    }
    
    /**
     * 创建组合动作 - 同时执行多个动作
     * @param actions 要执行的动作列表
     * @return 组合营销动作
     */
    public static MarketingAction compositeAction(List<MarketingAction> actions) {
        return order -> {
            StringBuilder message = new StringBuilder();
            boolean allSuccess = true;
            
            for (MarketingAction action : actions) {
                MarketingResult result = action.execute(order);
                allSuccess = allSuccess && result.isSuccess();
                message.append(result.getMessage()).append("; ");
            }
            
            return new MarketingResult(
                "COMPOSITE_ACTION", 
                "组合动作", 
                allSuccess, 
                message.toString()
            );
        };
    }
}    